<template>
	<view class="container">
		<image
			class="container-bg"
			src="/static/splash/splash_bg@1x.png"
			alt="有解开屏页背景"
			mode="scaleToFill"
		></image>
		<image
			class="logo"
			src="/static/splash/splash_logo.png"
			alt="有解logo"
			mode="aspectFit"
		></image>
		<image
			class="bottom-text"
			src="/static/splash/splash_bottom.png"
			alt="有解底部文字"
			mode="aspectFit"
		></image>
	</view>
</template>

<script lang="ts" setup>
import { onMounted } from "vue";
import API from "@/api/index";
import { getToken, setToken } from "@/utils/token";
import { useUserStore } from "@/store/modules/user";

const token = getToken();

// token操作
const userStore = useUserStore();

// 注册
const register = (registerParam: API.RegisterParam) => {
	API.user.queryRegister(registerParam).then((res) => {
		setToken(res.data?.token || "");
	});
};

// 获取当前用户信息
const getUserInfo = () => {
	API.user.queryMe().then((res) => {
		if (res.data) {
			const { heresolverType, username, membershipStatus } = res.data;
			userStore.setUserInfo({
				userType: heresolverType,
				userName: username,
				membershipStatus: membershipStatus,
			});
			if (heresolverType === "SOLVER") {
				// 跳转到index首页
				uni.redirectTo({
					url: "/pages/index/index-seeker",
				});
			} else if (heresolverType === "SEEKER") {
				// 跳转到index首页
				uni.redirectTo({
					url: "/pages/index/index-seeker",
				});
			} else {
				// 跳转到身份切换页
				uni.redirectTo({
					url: "/pages/identity-switch/index",
				});
			}
		}
	});
};

// 获取到code后请求后端服务，看这个用户是否已经在数据库中了，如果不在则需要注册
const loginWithCode = (body: API.LoginParam) => {
	API.user
		.queryLoginWithCode(body)
		.then((res) => {
			if (res.data) {
				setToken(res.data.token);
				userStore.setUserInfo({
					userId: res.data.userId,
				});
				getUserInfo();
			}
		})
		.catch((res) => {
			if (res.statusCode === 404) {
				uni.getUserInfo({
					provider: "weixin",
					success: (userInfoRes) => {
						const registerParams: API.RegisterParam = {
							openId: res.data.openId,
							username: userInfoRes.userInfo.nickName,
							heresolverType: "SOLVER",
							encryptedData: userInfoRes.encryptedData,
							iv: userInfoRes.iv,
							validityDays: 30,
						};
						register(registerParams);
					},
				});
			}
		});
};

// 一键登录
const loginIn = () => {
	uni.login({
		provider: "weixin",
		success: (res) => {
			loginWithCode({
				code: res.code,
				validityDays: 30,
			});
		},
	});
};

onMounted(() => {
	if (token) {
		loginIn();
	} else {
		setTimeout(() => {
			uni.redirectTo({
				url: "/pages/identity-switch/index",
			});
		}, 1000);
	}
});
</script>

<style lang="scss">
.container {
	position: relative;
	width: 100vw;
	height: 100vh;

	&-bg {
		position: fixed;
		top: 0;
		left: 0;
		display: block;
		width: 100%;
		height: 100%;
	}

	.logo {
		position: absolute;
		top: 20%;
		left: 50%;
		transform: translateX(-50%);
		width: 60%;
	}

	.bottom-text {
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 50%;
	}
}
</style>
